package leetcode_401_500;

import java.util.*;

public class LeeCode_423 {
    public static void main(String[] args) {
        System.out.println(originalDigits("fviefuro"));
    }
    private static String originalDigits(String s) {
        int[] arr = new int[26];
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            arr[c - 'a']++;
        }
        while (arr['g' - 'a'] > 0){
            arr['e' - 'a']--;
            arr['i' - 'a']--;
            arr['g' - 'a']--;
            arr['h' - 'a']--;
            arr['t' - 'a']--;
            list.add(8);
        }
        while (arr['u' - 'a'] > 0){
            arr['f' - 'a']--;
            arr['o' - 'a']--;
            arr['u' - 'a']--;
            arr['r' - 'a']--;
            list.add(4);
        }
        while (arr['w' - 'a'] > 0){
            arr['t' - 'a']--;
            arr['w' - 'a']--;
            arr['o' - 'a']--;
            list.add(2);
        }
        while (arr['x' - 'a'] > 0){
            arr['s' - 'a']--;
            arr['i' - 'a']--;
            arr['x' - 'a']--;
            list.add(6);
        }
        while (arr['z' - 'a'] > 0){
            arr['z' - 'a']--;
            arr['e' - 'a']--;
            arr['r' - 'a']--;
            arr['o' - 'a']--;
            list.add(0);
        }
        while (arr['h' - 'a'] > 0){
            arr['h' - 'a']--;
            list.add(3);
        }
        while (arr['f' - 'a'] > 0){
            arr['f' - 'a']--;
            arr['i' - 'a']--;
            arr['v' - 'a']--;
            arr['e' - 'a']--;
            list.add(5);
        }
        while (arr['s' - 'a'] > 0){
            arr['s' - 'a']--;
            arr['e' - 'a']--;
            arr['v' - 'a']--;
            arr['e' - 'a']--;
            arr['n' - 'a']--;
            list.add(7);
        }
        while (arr['o' - 'a'] > 0){
            arr['o' - 'a']--;
            arr['n' - 'a']--;
            arr['e' - 'a']--;
            list.add(1);
        }
        while (arr['i' - 'a'] > 0){
            arr['n' - 'a']--;
            arr['i' - 'a']--;
            arr['n' - 'a']--;
            arr['e' - 'a']--;
            list.add(9);
        }
        list.sort(Comparator.comparingInt(o -> o));
        StringBuilder sb = new StringBuilder();
        for (Integer num : list) {
            sb.append(num);
        }
        return sb.toString();
    }
}
